package com.faithbj.controller.statistics;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.faithbj.common.model.Region;
import com.faithbj.common.model.User;
import com.faithbj.common.utils.DateUtils;
import com.faithbj.model.conditions.InWorkTimeOverStatResult;
import com.faithbj.model.conditions.OutWorkTimeOverStatResult;
import com.faithbj.model.conditions.StreetOutTimeOverData;
import com.faithbj.model.conditions.WorkloadStatResult;
import com.faithbj.service.statistics.WorkStatService;

/**
 * 工作量与绩效统计
 * @author m
 *
 */
@Controller
@RequestMapping("/workStat")
public class WorkStatisticsController extends StatBaseController{
	
	@Resource
	WorkStatService workStatService;
	
	/**
	 * 核对工作量统计
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/workloadStat")
	public String workloadStat(HttpServletRequest request, ModelMap model) {
		String acceptStartTime = request.getParameter("acceptStartTime");
		String acceptEndTime = request.getParameter("acceptEndTime");
		Date from = null;
		Date to = null;
		if("1".equals(request.getParameter("type"))){
			to = new Date();
			from = this.getStartTime(to);
		} else {
			if(StringUtils.isNotBlank(acceptStartTime)){
				from = DateUtils.getStringToDate(acceptStartTime, DateUtils.DateFormatDefault);
			}
			if(StringUtils.isNotBlank(acceptEndTime)) {
				to = DateUtils.getStringToDate(acceptEndTime, DateUtils.DateFormatDefault);
			}
		}

		User user = this.getLoginUser();
		List<Region> countryRegion = this.getCountryRegionList(null, null);
		
		List<WorkloadStatResult> result = workStatService.getWorkloadData(from, to, user, countryRegion);
		
		model.put("resultList", result);
		return "/analysis/checkWorkloadStatistics";
	}
	
	/**
	 * 内网工作人员工作超时统计
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/inWorkTimeOverStat")
	public String inWorkTimeOverStat(HttpServletRequest request, ModelMap model) {
		String acceptStartTime = request.getParameter("acceptStartTime");
		String acceptEndTime = request.getParameter("acceptEndTime");
		Date from = null;
		Date to = null;
		if("1".equals(request.getParameter("type"))){
			to = new Date();
			from = this.getStartTime(to);
		} else {
			if(StringUtils.isNotBlank(acceptStartTime)){
				from = DateUtils.getStringToDate(acceptStartTime, DateUtils.DateFormatDefault);
			}
			if(StringUtils.isNotBlank(acceptEndTime)) {
				to = DateUtils.getStringToDate(acceptEndTime, DateUtils.DateFormatDefault);
			}
		}
		
		List<InWorkTimeOverStatResult> result = workStatService.getInWorkTimeOverData(from, to);
		
		model.put("resultList", result);
		return "/analysis/workOvertimeStatistics_inside";
	}

	/**
	 * 外网工作人员工作超时统计
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/outWorkTimeOverStat")
	public String outWorkTimeOverStat(HttpServletRequest request, ModelMap model) {
		//初始化区县列表
		this.initCountryRegionList();
		
		String acceptStartTime = request.getParameter("acceptStartTime");
		String acceptEndTime = request.getParameter("acceptEndTime");
		String countiesRegionId = request.getParameter("countiesRegionId");
		String streetRegionId = request.getParameter("streetRegionId");
		Date from = null;
		Date to = null;
		if("1".equals(request.getParameter("type"))){
			to = new Date();
			from = this.getStartTime(to);
		} else {
			if(StringUtils.isNotBlank(acceptStartTime)){
				from = DateUtils.getStringToDate(acceptStartTime, DateUtils.DateFormatDefault);
			}
			if(StringUtils.isNotBlank(acceptEndTime)) {
				to = DateUtils.getStringToDate(acceptEndTime, DateUtils.DateFormatDefault);
			}
		}
		
		User user = this.getLoginUser();
		List<Region> countryRegion = this.getCountryRegionList(countiesRegionId, streetRegionId);
		
		List<OutWorkTimeOverStatResult> result = workStatService.getOutWorkTimeOverData(from, to, user, countryRegion);
		
		model.put("resultList", result);
		model.put("countryRegionList", this.countryRegionList);
		return "/analysis/workOvertimeStatistics_external";
	}
}
